using System;
using System.Web.Mvc;
using Headspring.Training.Core.Model;
using Headspring.Training.Core.Services;
using Headspring.Training.UI.Controllers;
using Headspring.Training.UI.Models;
using MvcContrib;
using NUnit.Framework;
using NUnit.Framework.SyntaxHelpers;
using Rhino.Mocks;

namespace Headspring.Training.UnitTests.UI.Controllers
{
	[TestFixture]
	public class LoginControllerTester
	{
		public class FakeEmployeeRepository : IEmployeeRepository
		{
			public Employee GetByUserName(string userName)
			{
				throw new NotImplementedException();
			}

			public Employee[] GetAllEmployeesSorted()
			{
				return new[] {new Employee(), new Employee()};
			}
		}

		[Test]
		public void ShouldListAllEmployeesOnLoginPage()
		{
			IEmployeeRepository repository = new FakeEmployeeRepository();

			var controller = new LoginController(repository, null, null);
			ViewResult result = controller.Index();

			Assert.That(result.ViewName, Is.Null);
			Assert.That(result.ViewData.Contains<EmployeeSelectList>());
			var employees = result.ViewData.Get<EmployeeSelectList>();
			Assert.That(employees.GetListItems().Count, Is.EqualTo(2));
		}

		[Test]
		public void ShouldLogInWhenUsernameAndPasswordAreCorrect()
		{
			var employeeRepository = MockRepository.GenerateStub<IEmployeeRepository>();
			var authenticationService =
				MockRepository.GenerateStub<IAuthenticationService>();
			var userSession = MockRepository.GenerateMock<IUserSession>();
			var employee = new Employee("hsimpson", "Homer", "Simpson", "");

			employeeRepository.Stub(x => x.GetByUserName("hsimpson")).Return(employee);
			authenticationService.Stub(x => x.PasswordMatches(employee, "beer")).Return(
				true);
			userSession.Expect(x => x.LogIn(employee));

			var controller = new LoginController(employeeRepository,
			                                     authenticationService, userSession);

			controller.LogIn("hsimpson", "beer");

			userSession.VerifyAllExpectations();
		}
	}
}